package Sessions;


import javax.ejb.Stateless;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.jboss.ejb3.annotation.RemoteBinding;

import EntityBeans.Abilita;
import EntityBeans.EsperienzaLavorativa;
import EntityBeans.UtenteRegistrato;
/**
 * Session Bean implementation class AccessoSession
 */
@Stateless
@RemoteBinding(jndiBinding="AccessoSessionRemote")
public class AccessoSession implements AccessoSessionRemote {
	@PersistenceContext(unitName="swimv2")
	private EntityManager manager;
    /**
     * Default constructor. 
     */
    public AccessoSession() {
        // TODO Auto-generated constructor stub
    }
    
    
    
    @Override
	public ArrayList<UtenteRegistrato> cercaUtente(String email, String password) {
		String s = "FROM UtenteRegistrato u WHERE u.email=:email AND u.password=:password ";
		Query q = manager.createQuery(s);
		q.setParameter("email", email);
		q.setParameter("password", password);
		
		@SuppressWarnings("unchecked")
		ArrayList<UtenteRegistrato> utente = (ArrayList<UtenteRegistrato>) q.getResultList();
		if (utente.size()== 0)  return null ;
		else return utente;
	}
    
    @Override
    public ArrayList<EsperienzaLavorativa> recuperaEsperienze(String email){
    	
    	/*String s1 ="SELECT id FROM UtenteRegistrato u WHERE u.email=:email";
    	Query q1 = manager.createQuery(s1);
    	q1.setParameter("email", email);
    	int id = Integer.parseInt(q1.getSingleResult().toString());*/
    	
    	
    	String s1 ="FROM UtenteRegistrato ut WHERE ut.email=:email";
    	Query q1 = manager.createQuery(s1);
    	q1.setParameter("email", email);
    	UtenteRegistrato ut = (UtenteRegistrato) q1.getSingleResult();
    	String id = ut.getId();
    	
    	String s2 ="FROM EsperienzaLavorativa e WHERE e.idutente=:id";
    	Query q2 = manager.createQuery(s2);
    	q2.setParameter("id", id);
    	
    	@SuppressWarnings("unchecked")
		ArrayList<EsperienzaLavorativa> esperienzeUtente = (ArrayList<EsperienzaLavorativa>) q2.getResultList();
    	if (esperienzeUtente.size()==0)
    		return null;
    	else return esperienzeUtente;
    }
    
    @Override
    public ArrayList<Abilita> recuperaAbilita(String email){

    	/*String s1 = "SELECT id FROM UtenteRegistrato WHERE u.email=:email";
    	Query q1 = manager.createQuery(s1);
    	q1.setParameter("email", email);
    	int idUtente = Integer.parseInt(q1.getSingleResult().toString()) ;*/
    	
    	String s1 = "FROM UtenteRegistrato u WHERE u.email=:email";
    	Query q1 = manager.createQuery(s1);
    	q1.setParameter("email",email);
    	UtenteRegistrato ute = (UtenteRegistrato) q1.getSingleResult();
    	String idUtente = ute.getId();
    	
    	String s2 = "SELECT idabilita FROM PossessoAbilita p WHERE p.idabilita=:idUtente";
    	Query q2 = manager.createQuery(s2);
    	q2.setParameter("idUtente", idUtente);
    	String idAbilita = q2.getSingleResult().toString() ;
    	
    	String s3 = "FROM Abilita a WHERE a.id=:idAbilita";
    	Query q3 = manager.createQuery(s3);
    	q3.setParameter("idAbilita", idAbilita);
    	
    	@SuppressWarnings("unchecked")
		ArrayList<Abilita> abilitaUtente = (ArrayList<Abilita>) q3.getResultList();
    	if (abilitaUtente.size()==0) return null;
    	else return abilitaUtente;
    }

}
